package com.note7_12.question2;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class Question5 {

	/*
	 * 5:增加新商品的功能，要求商品号和商品名称都不能重复（即不能和已经存在的重复）
	 */
	public boolean equals(List<Product> list, int itemno, String name) {

		for (Product p : list) {
			if (p.getItemno() == itemno || p.getName().equals(name)) {
				return false;
			}
		}
		return true;
	}

	public boolean add(Product p) {
		boolean flag = true;
		String path = "src/com/note7_12/question2/products.xml";
		SAXReader reader = new SAXReader();
		XMLWriter xm = null;
		try {
			Document doc = reader.read(new File(path));
			Element root = doc.getRootElement();
			Element newp = root.addElement("product");
			if (!p.getFactoryaddr().equals("未知")) {
				newp.addAttribute("factoryaddr", p.getFactoryaddr());
			}
			String discount = "false";
			if (p.getDiscount()) {
				discount = "true";
			}
			newp.addAttribute("discount", discount);
			Element itemno = newp.addElement("itemno");
			itemno.setText(String.valueOf(p.getItemno()));
			Element name = newp.addElement("name");
			name.setText(p.getName());
			Element price = newp.addElement("price");
			price.setText(String.valueOf(p.getPrice()));
			Element stockcount = newp.addElement("stockcount");
			stockcount.setText(String.valueOf(p.getStockcount()));
			Element unit = newp.addElement("unit");
			unit.setText(p.getUnit());
			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setEncoding("UTF-8");
			xm = new XMLWriter(new FileOutputStream(path), format);
			xm.write(doc);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (xm != null) {
				try {
					xm.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return flag;
	}

	public void add(List<Product> list) {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入商品编号：");
		int itemno = in.nextInt();
		System.out.println("请输入商品名称：");
		String name = in.next();
		if (equals(list, itemno, name)) {
			System.out.println("请输入商品单价：");
			double price = in.nextDouble();
			System.out.println("请输入当前商品库存：");
			int stockcount = in.nextInt();
			System.out.println("请输入计价单位：");
			String unit = in.next();
			System.out.println("商品产地：");
			String factoryaddr = in.next();
			System.out.println("当前商品是否为特价商品？(y/n)");
			String str = in.next();
			boolean discount = false;
			if (str.equals("y")) {
				discount = true;
			}
			Product product = new Product(factoryaddr, discount, itemno, name, price, stockcount, unit);
			add(product);
			System.out.println("---添加完成！---");
		} else {
			System.out.println("---商品编号或名称重复---");
		}

	}

}
